using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class DAO_Question
    {
        public static bool Insert(Object tmp)
        {
            QUESTION obj = (QUESTION)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@QUESTION_DESCRIPTION", obj.QUESTION_DESCRIPTION));
            _list.Add(new SqlParameter("@QUESTION_LEVEL", obj.QUESTION_LEVEL));
            _list.Add(new SqlParameter("@QUESTION_TYPE", obj.QUESTION_TYPE));
            _list.Add(new SqlParameter("@USER_ID", obj.USER_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_insert", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static bool Update(Object tmp)
        {
            QUESTION obj = (QUESTION)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@QUESTION_DESCRIPTION", obj.QUESTION_DESCRIPTION));
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));
            _list.Add(new SqlParameter("@QUESTION_LEVEL", obj.QUESTION_LEVEL));
            _list.Add(new SqlParameter("@QUESTION_TYPE", obj.QUESTION_TYPE));
            _list.Add(new SqlParameter("@USER_ID", obj.USER_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_update", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static bool Delete(Object tmp)
        {
            QUESTION obj = (QUESTION)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            //_list.Add(new SqlParameter("@QUESTION_DESCRIPTION", obj.QUESTION_DESCRIPTION));
            _list.Add(new SqlParameter("@QUESTION_ID", obj.QUESTION_ID));
            //_list.Add(new SqlParameter("@QUESTION_LEVEL", obj.QUESTION_LEVEL));
            //_list.Add(new SqlParameter("@QUESTION_TYPE", obj.QUESTION_TYPE));
            //_list.Add(new SqlParameter("@USER_ID", obj.USER_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_question_delete", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static string CurrentInsertID()
        {
            List<QUESTION> _list = new List<QUESTION>();
            String sql = "Select * From dbo.QUESTION";
            DataTable table = clsDataAccessHelper.ExecuteQueryNonStoredProcedure(sql, null);
            if (table != null && table.Rows.Count > 0)
            {
                int max = 0;
                foreach (DataRow row in table.Rows)
                {
                    object item = row["QUESTION_ID"];
                    if(item != null) {
                        int _tmp = -1;
                        if (int.TryParse(item.ToString(), out _tmp))
                        {
                            if (_tmp > max) max = _tmp;
                        }
                    }
                }
                return max.ToString();
            }
            return "";
        }

        public static DataTable LoadAll()
        {
            List<QUESTION> _list = new List<QUESTION>();
            String sql = "Select * From dbo.QUESTION";
            DataTable table = clsDataAccessHelper.ExecuteQueryNonStoredProcedure(sql, null);
            return table;
        }

        public static DataTable Load(Object tmp)
        {
            QUESTION obj = (QUESTION)tmp;
            List<SqlParameter> _list = new List<SqlParameter>();
            if (!string.IsNullOrWhiteSpace(obj.USER_ID))
            {
                _list.Add(new SqlParameter("@USER_ID", obj.USER_ID));
            }
            else
            {
                _list.Add(new SqlParameter("@USER_ID", DBNull.Value));
            }

            if (!string.IsNullOrWhiteSpace(obj.QUESTION_LEVEL))
            {
                _list.Add(new SqlParameter("@QUESTION_LEVEL", obj.QUESTION_LEVEL));
            }
            else
            {
                _list.Add(new SqlParameter("@QUESTION_LEVEL", DBNull.Value));
            }

            if (!string.IsNullOrWhiteSpace(obj.QUESTION_TYPE))
            {
                _list.Add(new SqlParameter("@QUESTION_TYPE", obj.QUESTION_TYPE));
            }
            else
            {
                _list.Add(new SqlParameter("@QUESTION_TYPE", DBNull.Value));
            }

            return clsDataAccessHelper.ExecuteQuery("sp_question_load_filter", _list);
        }

		public static QUESTION Select(int id)
        {           
            List<SqlParameter> _list = new List<SqlParameter>();
            //_list.Add(new SqlParameter("@QUESTION_DESCRIPTION", obj.QUESTION_DESCRIPTION));
            _list.Add(new SqlParameter("@QUESTION_ID", id));
            //_list.Add(new SqlParameter("@QUESTION_LEVEL", obj.QUESTION_LEVEL));
            //_list.Add(new SqlParameter("@QUESTION_TYPE", obj.QUESTION_TYPE));
            //_list.Add(new SqlParameter("@USER_ID", obj.USER_ID));

            DataTable table = clsDataAccessHelper.ExecuteQuery("sp_question_select", _list);
            if (table != null && table.Rows.Count == 1)
            {
                QUESTION question = new QUESTION();
                question.QUESTION_DESCRIPTION = table.Rows[0]["QUESTION_DESCRIPTION"].ToString();
                question.QUESTION_ID = int.Parse(id.ToString());
                question.QUESTION_LEVEL = table.Rows[0]["QUESTION_LEVEL"].ToString();
                question.QUESTION_TYPE = table.Rows[0]["QUESTION_TYPE"].ToString();
                question.USER_ID = table.Rows[0]["USER_ID"].ToString();

                return question;
            }
            return null;
        }
    }
}
